package devTest.Paddle 
{
	import devTest.CSpriteMovement;
	import devTest.IInitEndProtocol;
	import flash.display.Bitmap;
	import flash.display.Stage;
	import flash.events.KeyboardEvent;
	import flash.geom.Point;
	import flash.xml.XMLDocument;

	/**
	 * ...
	 * @author miquel
	 */
	public class CPaddle extends CSpriteMovement implements IInitEndProtocol
	{
		//Bitmap	
		[Embed(source="../../../lib/images/paddle.png")]
		private var m_classPaddle:Class;
		
		//stage
		private var myStage:							Stage 	= null;
		
		//vector orientation of paddle (right or left)
		private var		m_iDividers:					uint	= 0;		//number of dividers to apply a new direction to ball
		
		//keycodes to up and down
		private var 	m_kKeyUp:						int		= 0;
		private var 	m_kKeyDown:						int		= 0;
		public function GetKeyUp		():				int				{	return m_kKeyUp;   }
		public function GetKeyDown		():				int				{	return m_kKeyDown; }
		public function SetPressedKeyUp (b:Boolean ):	void			{	pressUp = b;	   }
		public function SetPressedKeyDown(b:Boolean ):	void			{	pressDown = b;	   }
		
		public function GetDividers		():				uint			{   return m_iDividers; }
		
		
		/**
		 * constructor
		 */
		public function CPaddle			(s:Stage)				
		{	
			super();	
			myStage = s;
		}

		
		/**
		 * Init: Prepare object, read xml values and link the bitmap
		 * @param : the treenode xml with his information
		 * @return : if was created succesfully
		 */
		public function Init(_node:XML):Boolean
		{
			trace("object created: " + _node.@objclass);

			//read xml info 
			ReadXMLValues(_node);
			
			//create vector
			m_v2Direction   = null;
			m_v2Direction	= new Point();			
			InitVector();		

			//link bitmap with a sprite
			var l_bmpPaddle:Bitmap = null;
			l_bmpPaddle = new m_classPaddle();
			if (l_bmpPaddle != null)
			{
				trace("CPaddle: Init -> the bitmap is linked with the sprite: " + _node.@name );
				addChild(l_bmpPaddle);
			}
			else
			{
				trace("2:CPaddle: Init -> the bitmap is not linked with the sprite: " + _node.@name );
				return false;		
			}
			
			return true;	
		}
			
		
		/**
		 * Update: Procedure called each frame
		 */
		public function Update():void 
		{
			//moving the paddle
			Move();
		}
		

		/**
		 * Done: Procedure called to remove object (xml reload)
		 */
		public function Done():void
		{
			trace("3:CPaddle: Done -> removing Object");
		}
		
		
		/**
		 * ReadXMLValues: 
		 * @param : the treenode xml
		 */
		override public function ReadXMLValues(_node:XML):void
		{
			//read xml values
			super.ReadXMLValues(_node);
			
			m_iDividers		= _node.@div;
			
			m_kKeyUp		= _node.@up;
			m_kKeyDown 		= _node.@down;
		}
		
		
		/**
		 * Move: this method executes the movement of paddle
		 * @return : ever returns 0
		 */
		override public function Move():int
		{
			if(pressUp)			//Go Up
			{
				m_v2Direction.x = 0;
				m_v2Direction.y = -1;
			}
			else if(pressDown)	//Go Down
			{
				m_v2Direction.x = 0;
				m_v2Direction.y = 1;
			}
			else
			{
				m_v2Direction.x = 0;
				m_v2Direction.y = 0;
			}
			
			//move paddle
			//x += m_v2Direction.x * m_iSpeed;  //not used, only vertical movement
			y += m_v2Direction.y * m_iSpeed;
			
			//check limits top y bottom
			if (y + height > myStage.stageHeight)
				y = myStage.stageHeight - height;
			if	(y < 0)
				y = 0;	
					
			return 0;
		}
	}

}